package Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import GUI.GUIchatroom;
import GUI.GUIprivate;
import GUI.GUIstartpage;
import Mailbox.Mailbox;
import Mailbox.MailboxFTP;

public class ChatClient extends Thread {

	private Socket socket;
	private GUIstartpage sp;
	private ChatClientSendThread ccst;
	private MailboxFTP mbftp;

	public ChatClient(Socket socket, ChatClientSendThread ccst, MailboxFTP mbftp) {
		this.socket = socket;
		this.ccst = ccst;
		this.mbftp = mbftp;
	}

	public void setSP(GUIstartpage sp) {
		this.sp = sp;
	}

	public void run() {

		InputStream is = null;
		try {
			is = socket.getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader bf = new BufferedReader(isr);
		boolean shallRun = true;
		while (shallRun && !socket.isClosed() && socket.isConnected()) {
			try {

				String answer = bf.readLine();
				if (answer == null) {
					shallRun = false;
					;
				} else {
					if (answer.contains("Protocol::SendMessageChatroom")) {
						String[] roomMessage = answer.split(",");
						sp.displayMessage(roomMessage[1], roomMessage[2]);
					} else if (answer.contains("Protocol::NewRoom")) {
						int index = answer.indexOf(',');
						answer = answer.substring(index + 1, answer.length());
						sp.addRoom(answer);
					} else if (answer.contains("Protocol::sendAvailableRooms")) {
						String[] rooms = answer.split(",");
						for (int i = 1; i < rooms.length; i++) {
							sp.addRoom(rooms[i]);
						}
						ccst.send("updateAllnames");
					} else if (answer.contains("Protocol::GetNames")) {
						String[] names = answer.split(",");
						sp.updateRoom(names);
					} else if (answer.contains("Protocol::RemoveRoom")) {
						int index = answer.indexOf(',');
						answer = answer.substring(index + 1, answer.length());
						sp.removeRoom(answer);
					} else if (answer.contains("Protocol::AllNames")) {
						String[] names = answer.split(",");
						sp.updateAllNames(names);
					} else if (answer.contains("Protocol::AddName")) {
						String[] name = answer.split(",");
						sp.addName(name[1]);
					} else if (answer.contains("Protocol::RemoveName")) {
						String[] name = answer.split(",");
						sp.removeName(name[1]);
					} else if (answer.contains("Protocoll::Error")) {
						String[] error = answer.split(",");
						JOptionPane.showMessageDialog(null, error[1]);
						if (error[1].contains("Username already in use!")) {
							System.exit(0);
						} else if (error[1]
								.contains("The private room is full!")) {
							sp.closeRoom(error[2]);
						} else {
							System.out.println(error[1]);
							JOptionPane.showMessageDialog(null, error[1]);
						}

					} else if (answer.contains("Protocoll::StartFileRequest")) {
						String[] temp = answer.split(",");
						int n = JOptionPane
								.showConfirmDialog(
										null,
										"Your partner want to send you a file, do you accept?",
										"File", JOptionPane.YES_NO_OPTION);
						if (n == 0) {
							String s = JOptionPane.showInputDialog(null,
									"Enter the file of the name.", "");
							while (s == null) {
								s = JOptionPane.showInputDialog(null,
										"Enter the file of the name.", "");
							}
							FileReciever fr = new FileReciever(30006, s);
							ccst.send("FileAccepted," + temp[1] + ","
									+ fr.getAddress().getHostAddress());
							fr.start();
						} else {
							ccst.send("FileNotAccepted," + temp[1]);
						}

					} else if (answer.contains("Protocoll::AcceptedFile")) {
						FileSender fs = new FileSender(mbftp.removeFile());
						String[] temp = answer.split(",");
						fs.setReceiver(temp[1], 30006);
						fs.start();
					}

				}

			} catch (IOException e) {
				shallRun = false;
				e.printStackTrace();
			}

		}
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.exit(0);

	}

	public static void main(String[] args) {

	}

}